/**
 * Created by MyPC on 2017/7/16.
 */
let express = require('express');
let router = express.Router();
let logger = require('log4js').getLogger('billRoutes');
let BillModel = require('../../schemas/bill');
let BillTypeModel = require('../../schemas/billType');
let response = require('../../common/responseJson');

router.get('/', function (req, res, next) {
  res.redirect(301, '/res/html/bill/index.html');
});

router.get('/getAll', function (req, res, next) {
  let params = {};
  let startDay = req.query.startDay;
  let dayParams = {};
  if(startDay){
    dayParams['$gte'] = startDay;
  }
  let endDay = req.query.endDay;
  if(endDay){
    dayParams['$lte'] = endDay;
  }
  let query = BillModel;
  params['deleteTime'] = {'$eq': null};
  if(endDay || startDay){
    params['day'] = dayParams;
    query = BillModel.find(params);
  }else{
    query = BillModel.find(params).limit(50);
  }
  query = query.sort({'day': -1});
  query.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return response.fail(res, err);
  });
});

router.post('/', function (req, res, next) {
  let money = Number(req.body.money);
  if(!money>0){
    return response.fail(res, '请输入金额');
  }
  let billType = req.body.billType;
  if(!billType){
    return response.fail(res, '请选择类别');
  }
  let remarks = req.body.remarks;
  if(billType==='其他' && !remarks){
    return response.fail(res, '类别为其他时,请输入备注');
  }
  let day = req.body.day;
  if(!day){
    return response.fail(res, '请选择日期');
  }
  let promise = new Promise(function (resolve, reject) {
   BillModel.create({
      'money': money,
      'type': billType,
      'remarks': remarks,
      'day': day,
    }).then((result)=> {
     resolve(result);
   }).catch((err)=> {
     reject(err);
   })
  });
  promise.then(function (result) {
    console.log(result);
    response.success(res, result);
  }).catch(function (err) {
    logger.error('保存账单出错, error:%s', err);
    response.fail(res, '系统繁忙,请稍后再试');
  })
});

router.put('/:id', function (req, res, next) {
  let id = req.params.id;
  let money = Number(req.body.money);
  if(!money>0){
    return response.fail(res, '请输入金额');
  }
  let billType = req.body.billType;
  if(!billType){
    return response.fail(res, '请选择类别');
  }
  let remarks = req.body.remarks;
  if(billType==='其他' && !remarks){
    return response.fail(res, '类别为其他时,请输入备注');
  }
  let day = req.body.day;
  if(!day){
    return response.fail(res, '请选择日期');
  }
  let promise = new Promise(function (resolve, reject) {
    BillModel.update({'_id': id},{
      'money': money,
      'type': billType,
      'remarks': remarks,
      'day': day,
      'updateTime': new Date(),
    }).then((result)=> {
      resolve(result);
    }).catch((err)=> {
      reject(err);
    })
  });
  promise.then(function (result) {
    console.log(result);
    response.success(res, result);
  }).catch(function (err) {
    logger.error('修改账单出错, error:%s', err);
    response.fail(res, '系统繁忙,请稍后再试');
  })
});

module.exports = router;
